package com.ruoyi.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.dto.FaultManagementHandleDto;
import com.ruoyi.web.entity.FaultManagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.web.entity.FaultManagementHandle;
import com.ruoyi.web.service.IFaultManagementHandleService;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/admin/handle")
public class FaultManagementHandleController extends BaseController {

    private final IFaultManagementHandleService faultManagementHandleService;
    public FaultManagementHandleController(IFaultManagementHandleService faultManagementHandleServiceImpl){
        this.faultManagementHandleService = faultManagementHandleServiceImpl;
    }
    /**
     * 查询故障处理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:list')")
    @GetMapping("/list")
    public TableDataInfo list(FaultManagementHandle faultManagementHandle)
    {
        startPage();
        List<FaultManagementHandleDto> list = faultManagementHandleService.selectFaultManagementHandleList(faultManagementHandle);
        return getDataTable(list);
    }

    /**
     * 导出故障处理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:export')")
    @Log(title = "故障处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FaultManagementHandle faultManagementHandle)
    {
        List<FaultManagementHandleDto> faultManagementHandleDtos = faultManagementHandleService.selectFaultManagementHandleList(faultManagementHandle);
        ArrayList<FaultManagementHandle> faultManagementHandles = new ArrayList<>();
        for (FaultManagementHandleDto faultManagementHandleDto : faultManagementHandleDtos) {
            FaultManagementHandle temp = new FaultManagementHandle();
            BeanUtil.copyProperties(temp,faultManagementHandleDto);
            faultManagementHandles.add(temp);
        }
        ExcelUtil<FaultManagementHandle> util = new ExcelUtil<FaultManagementHandle>(FaultManagementHandle.class);
        util.exportExcel(response, faultManagementHandles, "故障处理数据");
    }

    /**
     * 获取故障处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(faultManagementHandleService.selectFaultManagementHandleById(id));
    }

    /**
     * 新增故障处理
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:add')")
    @Log(title = "故障处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FaultManagementHandle faultManagementHandle)
    {
        return toAjax(faultManagementHandleService.insertFaultManagementHandle(faultManagementHandle));
    }

    /**
     * 修改故障处理
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:edit')")
    @Log(title = "故障处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FaultManagementHandle faultManagementHandle)
    {
        return toAjax(faultManagementHandleService.updateFaultManagementHandle(faultManagementHandle));
    }

    /**
     * 删除故障处理
     */
    @PreAuthorize("@ss.hasPermi('admin:handle:remove')")
    @Log(title = "故障处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(faultManagementHandleService.deleteFaultManagementHandleByIds(ids));
    }

}
